package com.xbh.management.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbh.management.entity.ManRoleVO;
import com.xbh.management.entity.ManUserVO;
import com.xbh.management.mapper.RoleMapper;
import com.xbh.management.mapper.UserMapper;
import com.xbh.management.mapper.UserRoleMapper;
import com.xbh.management.model.ManRole;
import com.xbh.management.model.ManUser;
import com.xbh.management.model.ManUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-09-25 17:08
 */

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, ManUserRole> implements UserRoleService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 用户关联角色
     * @param roleIdList
     * @param userId
     * @param createPerson
     * @param updatePerson
     * @return
     */
    @Override
    public int insertUserRoleByIds(String roleIds, Long userId, String createPerson, String updatePerson, Date createTime,Date updateTime,Integer deleted) {

        String[] roleIdArray = roleIds.split(",");
        //用户传进来的角色，可能包含已经关联的角色id
        List<String> newRoleIdList = Arrays.stream(roleIdArray).collect(Collectors.toList());
        ManUser user = new ManUser();
        user.setUserId(userId);
        //查出没有关联的角色，则直接放行
        ManUserVO manUserVO = userMapper.queryUserRelationAllRole(user);
        if(manUserVO==null){
            return this.baseMapper.insertUserRoleByIds(newRoleIdList,userId,createPerson,updatePerson,createTime,updateTime,deleted);
        }
        //查出已关联的角色,将对象list转换为字符串list
        List<ManRole> oldRoleIdList = manUserVO.getRoleList();
        ArrayList<String> stringRoleIdList = new ArrayList<>();
        for (ManRole role : oldRoleIdList) {
            stringRoleIdList.add(role.getRoleId().toString());
        }
        //过滤掉已关联的
        List<String> reduce = newRoleIdList.stream().filter(item -> !stringRoleIdList.contains(item)).collect(Collectors.toList());

        return this.baseMapper.insertUserRoleByIds(reduce,userId,createPerson,updatePerson,createTime,updateTime,deleted);
    }

    /**
     * 给角色关联用户
     * @param userIds
     * @param roleId
     * @param createPerson
     * @param updatePerson
     * @param createTime
     * @param updateTime
     * @param deleted
     * @return
     */
    @Override
    public int insertRoleRelationUserByIds(String userIds, Long roleId, String createPerson, String updatePerson, Date createTime, Date updateTime, Integer deleted) {
        String[] userIdArray = userIds.split(",");
        //用户传进来的角色，可能包含已经关联的角色id
        List<String> newUserIdList = Arrays.stream(userIdArray).collect(Collectors.toList());
        ManRole manRole = new ManRole();
        manRole.setRoleId(roleId);
        //查出没有关联的角色，则直接放行
        ManRoleVO manRoleVO = roleMapper.queryRoleRelationAllUser(manRole);
        if(manRoleVO==null){
            return this.baseMapper.insertRoleRelationUserByIds(newUserIdList,roleId,createPerson,updatePerson,createTime,updateTime,deleted);
        }
        //查出已关联的角色,将对象list转换为字符串list
        List<ManUser> oldUserList = manRoleVO.getUserList();
        ArrayList<String> stringUserIdList = new ArrayList<>();
        for (ManUser user : oldUserList) {
            stringUserIdList.add(user.getUserId().toString());
        }
        //过滤掉已关联的
        List<String> reduce = newUserIdList.stream().filter(item -> !stringUserIdList.contains(item)).collect(Collectors.toList());

        return this.baseMapper.insertUserRoleByIds(reduce,roleId,createPerson,updatePerson,createTime,updateTime,deleted);
    }


    /**
     * 删除用户关联角色
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public int deleteUserRoleByIds(Long userId, Long roleId) {
        return this.baseMapper.deleteUserRoleByUserIdAndRoleId(userId, roleId);
    }


}
